//
//  QMCameraToolBar.m
//  QMCamera
//
//  Created by guoqingwei on 16/7/20.
//  Copyright © 2016年 cvte. All rights reserved.
//

#import "QMCameraToolBar.h"

static const CGFloat kButtonSize = 80.f;

@interface QMCameraToolBar ()

@end

@implementation QMCameraToolBar

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGSize buttonSize = (CGSize){kButtonSize, kButtonSize};
    BOOL isVertical = (CGRectGetWidth(self.bounds) < CGRectGetHeight(self.bounds));
    if (isVertical) {
        NSMutableArray *verticallyOrderedButtons = [NSMutableArray array];
        [verticallyOrderedButtons addObjectsFromArray:@[self.beforeProcessButton, self.shotButton, self.afterProcessButton]];
        [self layoutToolBarButtons:verticallyOrderedButtons buttonSize:buttonSize vertically:YES];
    } else {
        NSMutableArray *horizontallyOrderedButtons = [NSMutableArray array];
        [horizontallyOrderedButtons addObjectsFromArray:@[self.afterProcessButton, self.shotButton, self.beforeProcessButton]];
        [self layoutToolBarButtons:horizontallyOrderedButtons buttonSize:buttonSize vertically:NO];
    }
}

- (void)layoutToolBarButtons:(NSArray *)buttons buttonSize:(CGSize)size vertically:(BOOL)vertically
{
    NSInteger count = buttons.count;
    CGFloat fixedSize = vertically ? size.height : size.width;
    CGFloat maxLength = vertically ? CGRectGetHeight(self.bounds) : CGRectGetWidth(self.bounds);
    CGFloat stepPadding = maxLength / count;
    CGFloat padding = (maxLength - fixedSize * count) / (2 * count);
    
    for (NSInteger idx = 0; idx < count; idx++) {
        UIView *button = buttons[idx];
        CGFloat sameOffset = vertically ? (CGRectGetWidth(self.bounds) - CGRectGetWidth(button.bounds)) / 2 : (CGRectGetHeight(self.bounds) - CGRectGetHeight(button.bounds)) / 2;
        CGPoint origin = vertically ? CGPointMake(sameOffset, idx * stepPadding + padding) : CGPointMake(idx * stepPadding + padding, sameOffset);
        button.frame = (CGRect){origin, size};
    }
}

- (void)setup
{
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
    
    _afterProcessButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_afterProcessButton setTitle:@"After" forState:UIControlStateNormal];
    [_afterProcessButton addTarget:self action:@selector(buttonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_afterProcessButton];
    
    _shotButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _shotButton.tintColor = [UIColor redColor];
    [_shotButton setImage:[QMCameraToolBar shotButtonImage] forState:UIControlStateNormal];
    [_shotButton addTarget:self action:@selector(buttonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_shotButton];
    
    _beforeProcessButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_beforeProcessButton setTitle:@"Before" forState:UIControlStateNormal];
    [_beforeProcessButton addTarget:self action:@selector(buttonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_beforeProcessButton];
}

- (void)buttonDidClicked:(id)sender
{
    if (sender == self.beforeProcessButton) {
        if (self.beforeProcessButtonTapped) {
            self.beforeProcessButtonTapped();
        }
    } else if (sender == self.shotButton) {
        if (self.shotButtonTapped) {
            self.shotButtonTapped();
        }
    } else if (sender == self.afterProcessButton) {
        if (self.afterProcessButtonTapped) {
            self.afterProcessButtonTapped();
        }
    }
}

+ (UIImage *)shotButtonImage
{
    return [UIImage imageNamed:@"shot"];
}

@end
